package models

import (
	"golanger.com/log"
	. "golanger.com/middleware"
	"golanger.com/utils"
	"labix.org/v2/mgo"

	"helper"
)

const DBUSE = "db"
const DBGLOBAL = "dbglobal"

type JKDB struct {
	m *helper.Mongo
}

func New(db string) *JKDB {
	m, o := Middleware.Get(db).(*helper.Mongo)
	if o == true {
		return &JKDB{
			m: m,
		}
	}
	return nil
}

/* get all database names */
func (jk *JKDB) JKDBS() []string {
	names, err := jk.m.DBSession().DatabaseNames()
	if err != nil {
		log.Errorln("Get dbs error")
	}
	return names
}

/* get all collection names */
func (jk *JKDB) JKCollections(dbname string) []string {
	c, err := jk.m.DB(dbname).CollectionNames()
	if err == nil {
		return c
	}
	var str []string
	return str
}

func JKGetCollection(db string, dbcol utils.M) *mgo.Collection {
	mgo, _ := Middleware.Get(db).(*helper.Mongo)
	return mgo.C(dbcol)
}
